<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>

        Promise.allSettled2 = (promises) => {
            return new Promise((resolve, reject) => {
                let len = promises.length
                let count = 1
                const results = new Array(len)
                promises.forEach((promise, index) => {
                    // 对p进行一次包装，防止非Promise对象
                    Promise.resolve(promise).then(res => {
                        results[index] = {
                            status: 'fulfilled',
                            value: res
                        }
                        if (count++ === len) resolve(results)
                    }).catch(err => {
                        results[index] = {
                            status: 'rejected',
                            value: err
                        }
                        if (count++ === len) resolve(results)
                    })

                });
            })
        }

        const p1 = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve(1)
            }, 1000)
        })
        const p2 = Promise.resolve(2)
        const p3 = 3
        const p4 = new Date()
        const p5 = Promise.reject(111)
        const promises = [p1, p2, p3, p4, p5]

        const pp = Promise.allSettled(promises).then(res => {
            console.log(res)
        })


        const pp2 = Promise.allSettled2(promises).then(res => {
            console.log(res)
        })
    </script>
</body>

</html>